<?php

namespace App\Http\Controllers\Payment;

use App\Http\Controllers\Controller;
use App\Models\Common\ApiResponse;
use App\Models\Invoice\Subject;
use Illuminate\Http\Request;

class InvoiceSubjectController extends Controller
{

    public $name = '开票主体';

    public function index()
    {
//        $this->admin = $user = admin();
//        $this->logContent = '查看开票主体';
        $parkingLotID = getParkingLotID();

        return ApiResponse::return('', Subject::where('parking_lot_id', $parkingLotID)->get());
    }

    public function store(Request $request)
    {
        $this->admin = $user = admin();
        $this->parkingLotID = $parkingLotID = getParkingLotID();
        $this->logContent = '编辑开票主体';

        if ($id = $request->get('id')) {
            $subject = Subject::findOrFail($id);
        } else {
            $subject = new Subject();
            $subject->id = \Str::orderedUuid()->toString();
            $subject->parking_lot_id = $parkingLotID;
        }

        $subject->type = Subject::TYPE_CHINA_UMS;
        $subject->type_cn = \Arr::get(Subject::TYPE_CNS, Subject::TYPE_CHINA_UMS);
        $subject->fill($request->only(['name', 'remark', 'enabled']));

        if (!$subject->name) {
            $subject->name = $subject->type_cn;
        }

        if (!$msgSrc = $request->get('msgSrc')) {
            return ApiResponse::fail('消息来源必填');
        }

        if (!$merchantId = $request->get('merchantId')) {
            return ApiResponse::fail('测试商户号必填');
        }

        if (!$terminalId = $request->get('terminalId')) {
            return ApiResponse::fail('测试终端号必填');
        }

        if (!$key = $request->get('key')) {
            return ApiResponse::fail('签名密钥必填');
        }

        $subject->parameters = [
            'merWxAppId' => strval($request->get('merWxAppId')),
            'merWxOrderId' => strval($request->get('merWxOrderId')),
            'alipayUserId' => strval($request->get('alipayUserId')),
            'payee' => strval($request->get('payee')),
            'checker' => strval($request->get('checker')),
            'drawer' => strval($request->get('drawer')),
            'msgSrc' => $msgSrc,
            'merchantId' => $merchantId,
            'terminalId' => $terminalId,
            'key' => $key
        ];

        if (Subject::query()->where('parking_lot_id', $parkingLotID)->where('type', $subject->type)->where('id', '<>', $subject->id)->exists()) {
            return ApiResponse::fail('不可重复添加相同收费主体的配置');
        }

        if (\DB::transaction(function () use ($subject) {
            if ($subject->enabled) {
                Subject::where('parking_lot_id', $subject->parking_lot_id)->where('id', '<>', $subject->id)->update(['enabled' => false]);
            }
            return $subject->save();
        })) {
            return ApiResponse::return();
        }

        return ApiResponse::return('', null, ApiResponse::RESP_CODE_FAILURE);
    }

    public function show($id)
    {
//        $this->admin = $user = admin();
        $parkingLotID = getParkingLotID();
//        $this->logContent = '查看开票主体详情';
        return ApiResponse::return('', Subject::where('parking_lot_id', $parkingLotID)->findOrFail($id));
    }

    public function destroy($id)
    {
        $this->admin = $user = admin();
        $this->parkingLotID = $parkingLotID = getParkingLotID();
        $this->logContent = '删除开票主体';

        if (!Subject::query()->where('parking_lot_id', $parkingLotID)->find($id)) {
            return ApiResponse::return();
        }

        if (Subject::where('parking_lot_id', $parkingLotID)->where('id', $id)->delete()) {
            return ApiResponse::return();
        }

        return ApiResponse::return('', null, ApiResponse::RESP_CODE_FAILURE);
    }
}
